package com.kandinfo.isbnmanage.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kandinfo.base.common.constant.ResultCode;
import com.kandinfo.base.common.dto.ResultDto;
import com.kandinfo.base.common.dto.ResultDtoFactory;
import com.kandinfo.isbnmanage.dto.IsbnInformationDto;
import com.kandinfo.isbnmanage.service.IsbnInformationService;

/**
 * @author tangqh
 */
@Controller
@RequestMapping(value = "/isbninformation")
public class IsbnInformationController {
	
	private final static Logger LOGGER = LoggerFactory.getLogger(IsbnInformationController.class);

	@Autowired
	IsbnInformationService isbnInformationService;
    
    /**
     * ISBN对应的基本信息
     */
    @RequestMapping(value = "/index", method=RequestMethod.GET)
	public String index(Model model,  String workType, String barcode,String isbnInformationId) {
		model.addAttribute("barcode", barcode);
		model.addAttribute("isbnInformationId", isbnInformationId);
		return "isbnmanage/editor/isbn_information";
	}
	
    /**
     * 根据inforId查已上传的图片
     * 
     * @param isbnInformationId
     * @return
     */
    @RequestMapping(value = "/getInformationView", method = RequestMethod.POST)
    public String findInformationByInforId(@RequestParam Long isbnInformationId, Model model) {
    	return "isbnmanage/preview/isbn_view_information";
    }
    
	 /**
     * 根据barcode查询isbnInformationId个数
     */
    @RequestMapping(value = "/getIsbnInformation", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto getIsbnInformation(String barcode,Long isbnInformationId) {
		return ResultDtoFactory.createResult(ResultCode.SUCCESS, "数据查询成功", isbnInformationService.findByBarcodeAndInforId(barcode, isbnInformationId));
    }
    
    @RequestMapping(value = "/getInformationData", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto getInformationData(@RequestParam Long isbnInformationId) {
		return ResultDtoFactory.createResult(ResultCode.SUCCESS, "数据查询成功", isbnInformationService.findInforByInforId(isbnInformationId));
    }
    
    /**
     * 
     * 创建新的空IsbnInformation
     * 
     * @return
     */
    @RequestMapping(value = "/newInformation", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto newInformation(@RequestBody IsbnInformationDto isbnInformationResDto) {
		try {
			return ResultDtoFactory.createResult(ResultCode.SUCCESS, "数据查询成功", isbnInformationService.newInformation(isbnInformationResDto));
		} catch (Exception e) {
			LOGGER.error("newInformation ERROR!" , e);
			return ResultDtoFactory.createResult(ResultCode.ERROR, e.getMessage());
		}
    }
    
}
